package com.example.alladapter.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.alladapter.Adapter.RecycleViewAdapter;
import com.example.alladapter.Adapter.myDialog;
import com.example.alladapter.Datas.PicName;
import com.example.alladapter.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment3#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment3 extends Fragment {
    /***
     * 图片浏览
     */
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private int[] pics = {R.mipmap.pic1, R.mipmap.pic9, R.mipmap.pic3, R.mipmap.pic4, R.mipmap.pic10,
            R.mipmap.pic6, R.mipmap.pic7, R.mipmap.pic8, R.mipmap.tq1, R.mipmap.tq2,
            R.mipmap.tq3, R.mipmap.tq4, R.mipmap.tq5, R.mipmap.tq6, R.mipmap.tq7,
            R.mipmap.tq8, R.mipmap.tq8};
    private String[] strings = {"远观图", "风景图", "水边驾船图", "黑暗深林", "空笼图", "夜中精灵", "红衣女士", "涂片", "图片1", "涂片",
            "图片1", "涂片", "图片1", "涂片", "图片1", "涂片", "图片1"};
    private ArrayList<PicName> arrayList;
    private RecyclerView recyclerView;
    private Context mContext;
    private int Kpos;
    private myDialog myDialog;

    public Fragment3() {
        // Required empty public constructor
    }

    public Fragment3(Context mContext) {
        this.mContext = mContext;
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment3.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment3 newInstance(String param1, String param2) {
        Fragment3 fragment = new Fragment3();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        PicName picName;
        arrayList = new ArrayList<>();
        arrayList.clear();
        for (int i = 0; i < pics.length; i++) {
            picName = new PicName(pics[i], strings[i]);
            arrayList.add(picName);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_3, container, false);
        initView(view);
        setrecAdapter();
        return view;
    }

    private void setrecAdapter() {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        RecycleViewAdapter recycleViewAdapter = new RecycleViewAdapter(1, mContext, new RecycleViewAdapter.OnRecycleClickListen() {
            @Override
            public void Onclick(int pos) {
                myDialog = new myDialog(mContext, pos, arrayList);
                Kpos = pos;
                myDialog.setOnLeftClick(new myDialog.OnClickLeftListen() {
                    @Override
                    public Void OnLeftClick(com.example.alladapter.Adapter.myDialog myDialog) {
                        if (Kpos <= 0) {
                            Log.d("测试结果", "第一张" + Kpos);
                            myDialog.setPicTitle(arrayList.get(Kpos).string + "(第一张图片了)");
                            Kpos = 0;
                        } else {
                            Kpos--;
                            Log.d("测试结果", "点击了左键" + Kpos);
                            myDialog.setPicTitle(arrayList.get(Kpos)
                                    .string).setPicId(arrayList.get(Kpos).pic);
                        }
                        return null;
                    }
                })
                        .setOnRightListen(new myDialog.OnClickRightListen() {
                            @Override
                            public Void OnRightClick(com.example.alladapter.Adapter.myDialog myDialog) {
                                if (Kpos >= arrayList.size() - 1) {
                                    myDialog.setPicTitle(arrayList.get(Kpos).string + "(最后一张图片了)");
                                    Log.d("测试结果", "最后一张" + Kpos);
                                } else {
                                    Kpos++;
                                    Log.d("测试结果", "点击了右键" + Kpos);
                                    myDialog.setPicTitle(arrayList.get(Kpos).string)
                                            .setPicId(arrayList.get(Kpos).pic);
                                }
                                return null;
                            }
                        })
                        .show();
            }
        }, arrayList);
        recyclerView.setAdapter(recycleViewAdapter);
    }

    private void initView(View view) {
        recyclerView = view.findViewById(R.id.FRec3);
    }
}